﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyRabbit.Application.Command;
using MyRabbit.Application.Dtos;
using MyRabbit.Application.ErrorCode;

namespace MyRabbit.Web.Api.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IMediator mediator;
        public UserController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="userPass">用户密码</param>
        /// <returns></returns>

        [HttpGet]
        public async Task<APIResult<UserDTO>> Login(string? userName, string? userPass)
        {
            LoginQueryCommand command = new LoginQueryCommand();
            command.UserName = userName;
            command.UserPass = userPass;
            return await mediator.Send(command);
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddUser(UserAddCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 上传图片（头像）
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<string> UpLoad(IFormFile file)
        {
            var fileName=file.FileName;
            var path=Directory.GetCurrentDirectory()+@"/wwwroot/Pics/"+file.FileName;
            using(FileStream fs=new FileStream(path,FileMode.Create))
            {
                await file.CopyToAsync(fs);
                fs.Flush();
            }
            return "https://localhost:44352/Pics/"+  fileName;
        }
        
        




    }
}
